package com.sgb.kjwl.view.ui.adapter.lease;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.CarBrandEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter2;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.PinyinUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 车辆发布品牌选择适配器
 */
public class CarSideBarAdapter extends BaseRecycleAdapter2<CarBrandEntity> {

    private TextView tvCatalog;
    private TextView tvName;

    public CarSideBarAdapter(Context context, List<CarBrandEntity> datas) {
        super(context, R.layout.item_car_brand, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final CarBrandEntity entity, int position) {
        holder.setBinding(BR.viewModel, entity);
        tvCatalog = holder.getItem().findViewById(R.id.catalog);
        tvName = holder.getItem().findViewById(R.id.name);
        tvName.setText(entity.getName());
        //如果当前位置等于该分类首字母的Char的位置 ，则认为是第一次出现
        if (position == getPositionForSection(entity.getFirstLetter())) {
            tvCatalog.setVisibility(View.VISIBLE);
            tvCatalog.setText(entity.getFirstLetter().toUpperCase());
        } else {
            tvCatalog.setVisibility(View.GONE);
        }
    }

    /**
     * 获取catalog首次出现位置
     */
    public int getPositionForSection(String catalog) {
        for (int i = 0; i < getDatas().size(); i++) {
            String sortStr = getDatas().get(i).getFirstLetter();
            if (catalog.equalsIgnoreCase(sortStr)) {
                return i;
            }
        }
        return -1;
    }

}